﻿Imports Microsoft.VisualBasic
Imports System.Data

<System.ComponentModel.DataObjectAttribute(True)> _
Public Class BLL_SearchVOrganization

    Public Sub New()
        ' 
        ' TODO: 在此加入建構函式的程式碼 
        ' 
    End Sub

    'SELECT 
    <System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.[Select])> _
    Public Function BLL_Select(ByVal VOrganizationID As String, ByVal directUOrganizationID As String) As DataTable

        Dim sql As New MyOrganizationDataFilter
        Dim dat As New clsData

        sql.AppendFormat("SELECT A.VOrganizationID,A.UOrganizationID,A.VOrganizationName,isnull(B.PersonCount,0) as PersonCount ")
        sql.AppendFormat(",vl.VolunteerName as LeaderName, vl.OfficePhone as LeaderPhone, A.ServiceType   ")
        sql.AppendFormat(",case when A.Locked='Y' then '1' else '0' end as IsUnLock ")
        sql.AppendFormat(",case when A.Locked ='Y' then '0' else '1' end as IsLock ")
        sql.AppendFormat("FROM VOrganizationViewV3 A ")
        sql.AppendFormat("left join VolunteerLeader vl on A.UOrganizationID = vl.UOrganizationID ")
        sql.AppendFormat("left join (select VOrganizationID, COUNT(B.VolunteerID) as PersonCount from VolunteerView B ")
        sql.AppendFormat("GROUP BY VOrganizationID ")
        sql.AppendFormat(") B on A.VOrganizationID=B.VOrganizationID ")
        sql.AppendFormat("WHERE 1=1 ")

        If VOrganizationID <> "" Then
            sql.AppendFormat("and A. VOrganizationID = '{0}' ", VOrganizationID)
        End If

        If directUOrganizationID <> "" Then
            sql.AppendFormat("and A.DirectUOrganizationID = '{0}' ", directUOrganizationID)
        End If
        sql.AppendFormat("order by VOrganizationName ")  'Add By Angus 2010/05/18
        Return dat.GetData(sql)

    End Function

    'Update 
    <System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Update)> _
    Public Function BLL_Update(ByVal ServiceType As String, ByVal original_VOrganizationID As Integer) As Integer

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope

            sql.AppendFormat("UPDATE VOrganization SET ")
            sql.AppendFormat(" ServiceType='{0}'", ServiceType)
            sql.AppendFormat("WHERE VOrganizationID={0}; ", original_VOrganizationID)
            dat.AccData(sql)

            scope.Complete()

            Return 1

        End Using

    End Function

End Class
